package com.xj.mydemo.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xj.mydemo.Base.BaseActivity;
import com.xj.mydemo.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private final static String TAG = "mainActivity";

    private ViewPager mViewPager;
    private View view1, view2, view3;
    private List<View> mList;
    /**
     * 检查更新按钮
     */
    private Button BtnUpdate;
    /**
     * 界面跳转按钮
     */
    private Button BtnIntent;

    private Toolbar mToolbar;
    private TextView resultTV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        initData();
        resultTV = (TextView) findViewById(R.id.textView);
        BtnIntent = (Button) findViewById(R.id.button);
        BtnUpdate = (Button) findViewById(R.id.btn_update);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        BtnIntent.setOnClickListener(this);
        BtnUpdate.setOnClickListener(this);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mToolbar.setTitle("Demo");
        setSupportActionBar(mToolbar);

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mList.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                // TODO Auto-generated method stub
                container.removeView(mList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(mList.get(position));


                return mList.get(position);
            }

        };
        mViewPager.setAdapter(pagerAdapter);
    }

    private void initData() {
        LayoutInflater inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.layout1, null);
        view2 = inflater.inflate(R.layout.layout2, null);
        view3 = inflater.inflate(R.layout.layout3, null);
        mList = new ArrayList<View>();
        mList.add(view1);
        mList.add(view2);
        mList.add(view3);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {

            case R.id.btn_update:
                intent.setClass(MainActivity.this, UpdateActivity.class);
                startActivity(intent);
                break;
            case R.id.button:

                intent.setClass(MainActivity.this, TabActivity.class);
                startActivity(intent);
                break;
        }
    }


}
